package com.termux.project;
import java.io.*;
import com.termux.app.*;
import com.termux.util.*;

class Project
{
	static final String OUTFILE=TermuxService.FILES_PATH + "/tmp/a.out";
	String cmake_minimum_required = "3.10";
	public String name;
	public String id;
	public String projectRootDir;
	public String type;
	public Project(String name,String id,String rootDir){

		this.name = name;
		this.id = id;
		this.projectRootDir = rootDir;
		this.libs = new StringBuilder();
		this.CFLAGS = new StringBuilder();
		this.CXXFLAG = new StringBuilder();
		this.srcFiles = new StringBuilder();
		this.type = "Console";
	}
	public StringBuilder libs;
	public StringBuilder srcFiles;
	public StringBuilder CFLAGS;
	public StringBuilder CXXFLAG;
	public void putLibs(String ... libs){
		for (String lib:libs){
			this.libs.append(" ").append(lib);
		}
	}
	public boolean generateCMakeFile(){
		StringBuilder builder = new StringBuilder();
		libs.append(" ");
		CFLAGS.append(" ");
		CXXFLAG.append(" ");
		String outType = !type.equals("Console") ? "add_library(" + name + " SHARED ":"add_executable(" + name;
		builder.append("cmake_minimum_required(VERSION ").append(cmake_minimum_required).append(")\n");
		builder.append("set(CMAKE_C_FLAGS \"-Wall -Wextra ").append(CFLAGS.toString()).append("\")\n");
		builder.append("project(").append(name).append(")\n");
		builder.append("aux_source_directory(").append("Srcs ").append("SRC_LIST)\n");
		builder.append("include_directories(").append("Headers").append(")\n");
		builder.append(outType).append(" ${SRC_LIST})\n");
		builder.append("target_link_libraries(").append(name).append(" ").append(libs.toString()).append(")\n");
		builder.append("set (LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/lib)");

		File cmakeFile = new File(projectRootDir,"CMakeLists.txt");
		try
		{
			if (!cmakeFile.exists())
				cmakeFile.createNewFile();
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return false;
		}
		PrintStream stream = null;
		try
		{
			stream = new PrintStream(cmakeFile.getAbsolutePath());
			stream.print(builder.toString());
		}
		catch (FileNotFoundException e)
		{
			Logger.d(e);
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
